package com.example.manager;

import java.io.IOException;
import java.util.ArrayList;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.debug.Debug;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.util.Log;

import com.example.fruit.GameActivity;
import com.example.input.Score;


public class ResourcesManager {
	private static final ResourcesManager INSTANCE = new ResourcesManager();

	public Engine engine;
	public GameActivity activity;
	public BoundCamera camera;
	public VertexBufferObjectManager vbom;
	public ITextureRegion splash_region;
	//private BitmapTextureAtlas splashTextureAtlas;
	BuildableBitmapTextureAtlas splashTextureAtlas;
	BuildableBitmapTextureAtlas highscoreTextureAtlas;
	BuildableBitmapTextureAtlas loginTextureAtlas;
	public ITiledTextureRegion a_region,b_region,c_region,h_region,r_region,t_region;
	public ITiledTextureRegion rLeft,rRight;
	
	public ITextureRegion menu_background_region;
	public ITextureRegion game_background_region;
	public ITextureRegion play_region;
	public ITextureRegion highscore_region;
	public ITextureRegion setting_region;
	public ITextureRegion help_region;
	public ITextureRegion login_region;
	public ITextureRegion timemode_region;
	public ITextureRegion ball_region;
	public ITextureRegion boom_region;
	public ITextureRegion exit_region;
	public ITextureRegion newgame_region;
	public ITextureRegion text_region;
	public ITextureRegion new_region;
	public ITextureRegion ninja_region;
	public ITextureRegion slice_region;
	public ITextureRegion backscore_region,btnscore_region,bar_region;
	public ITextureRegion backlogin_region,btnlogin_region,btnregister_region;
	public ITextureRegion mTextureRegionLevel;
	public ITextureRegion score_region;
	public ITextureRegion star_region,star1_region;
	public ITextureRegion playgame_region;
	public ITextureRegion restartgame_region;
	public ITextureRegion updatescore_region;
	public ITextureRegion backresault_region;
	public ITextureRegion quitgame_region;
	public ITextureRegion titlepausegame_region;
	public ITextureRegion retry_region;
	public ITextureRegion waiting_region;
	public ITextureRegion topscore_region;
	public ITextureRegion backbar_region;
	public ITextureRegion oldman_region;
	public ITextureRegion bananacold_region;
	public ITextureRegion bananax2_region;
	public ITextureRegion bananafrenzy_region;
	public ITextureRegion boomspec_region;
	public ITextureRegion cong10_region;
	public ITextureRegion tru10_region;
	public ITextureRegion x2_region;
	public ITextureRegion streak_region;
	public ITextureRegion offline_region;
	public ITextureRegion freeze_region,frenzy_region;
	
	private BuildableBitmapTextureAtlas ballTextureAtlas;
	private BuildableBitmapTextureAtlas textTextureAtlas;
	private BuildableBitmapTextureAtlas backgameTextureAtlas;
	private BuildableBitmapTextureAtlas menuTextureAtlas;
	private BuildableBitmapTextureAtlas menuResaultTextureAtlas;
	private BuildableBitmapTextureAtlas textSpecialTextureAtlas;
	
	public ITiledTextureRegion mTiledTextureRegionMusic;
	public ITiledTextureRegion mTiledTextureRegionSound;
	public ITiledTextureRegion mTiledTextureRegionX;
	public ITiledTextureRegion mTiledTextureRegionUpdate;
	public ITiledTextureRegion mTiledTextureRegionTopScoreWait;
	public ITiledTextureRegion mTiledTextureRegionPlay;
	public ITiledTextureRegion mTiledTextureRegionResume;
	
	public ITiledTextureRegion mTiledTextureRegionRingLeader;	
	public ITiledTextureRegion mTiledTextureRegionRingLeadergiftbox1;	
	public ITiledTextureRegion mTiledTextureRegionRingLeadergiftbox2;	
	public ITiledTextureRegion mTiledTextureRegionRingLeadergiftbox3;
	
	public ITiledTextureRegion mTiledTextureRegionA;
	public ITiledTextureRegion mTiledTextureRegionB;
	public ITiledTextureRegion mTiledTextureRegionC;
	public ITiledTextureRegion mTiledTextureRegionD;
	public ITiledTextureRegion mTiledTextureRegionE;
	public ITiledTextureRegion mTiledTextureRegionF;
	public ITiledTextureRegion mTiledTextureRegionG;
	public ITiledTextureRegion mTiledTextureRegionH;
	public ITiledTextureRegion mTiledTextureRegionI;
	public ITiledTextureRegion mTiledTextureRegionJ;
	public ITiledTextureRegion mTiledTextureRegionK;
	public ITiledTextureRegion mTiledTextureRegionM;
	public ITiledTextureRegion mTiledTextureRegionN;
	
	public Font font,fonttextx2;
	public float wRate;
	public float hRate;
	
	private SharedPreferences audioOptions, scores,login;
	private SharedPreferences.Editor audioEditor, scoresEditor,loginEditor;
	
	public ArrayList<ITiledTextureRegion> mArrFruit = new ArrayList<ITiledTextureRegion>();
	public ArrayList<String> mArrFruitId = new ArrayList<String>();
	public ArrayList<ITextureRegion> mArrBoom = new ArrayList<ITextureRegion>();
	public ArrayList<String> mArrBoomId = new ArrayList<String>();
	
	public static ResourcesManager getInstance() {
		return INSTANCE;
	}

	public void loadMenuResources() {
		loadMenuGraphics();
		loadMenuResault();
		loadMenuAudio();
		loadMusicGame();
		loadMenuFonts();
		createWidthHeighRate(); 
		loadHighscoreGraphics();
	}

	public void loadGameResources() { 
		loadGameAudio();
		loadGameGraphics();
		createArrayFruit();
		createArrayBoom();
		createWidthHeighRate(); 
		loadGameFonts();
		loadGameAudio();
	}
	public void loadHighScores() {
		loadMenuFonts();
		
	}
	private void createArrayBoom() {
		mArrBoom.add(boom_region);
		mArrBoomId.add("BOOM");
		
	}

	private void createWidthHeighRate() {
		wRate = (float) (1.0*activity.CAMERA_WIDTH/activity.width);
		hRate = (float) (1.0*activity.CAMERA_HEIGHT/activity.height);
	}

	private void createArrayFruit() {
		mArrFruit.add(a_region);
		mArrFruitId.add("A");
		mArrFruit.add(b_region);
		mArrFruitId.add("B");
		mArrFruit.add(c_region);
		mArrFruitId.add("C");
		mArrFruit.add(h_region);
		mArrFruitId.add("H");
		mArrFruit.add(r_region);
		mArrFruitId.add("R");
		mArrFruit.add(t_region);
		mArrFruitId.add("T");
		
	}
	private void loadMenuResault()
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("menu/");
		menuResaultTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.DEFAULT);
		updatescore_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuResaultTextureAtlas, activity, "updatescore.png");
		backresault_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuResaultTextureAtlas, activity, "back.png");
		retry_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuResaultTextureAtlas, activity, "retry.png");
		try 
		{
		    this.menuResaultTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
		    this.menuResaultTextureAtlas.load();
		} 
		catch (final TextureAtlasBuilderException e)
		{
		        Debug.e(e);
		}
	}
	private void loadMenuGraphics() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("menu/");
		menuTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.DEFAULT);
		menu_background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "backgroundmenu.png");
		play_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "play.png");
		highscore_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "highscore.png");
		exit_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "exit.png");
		newgame_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "newgame.png");
		text_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "text.png");
		ninja_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "ninja.png");
		slice_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "slice.png");
		new_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "new.png");
		setting_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "setting.png");
		help_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "help.png");
		login_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "login.png");
		timemode_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "timemode.png");
		loadTiledTextureRegion();
		try 
		{
		    this.menuTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
		    this.menuTextureAtlas.load();
		} 
		catch (final TextureAtlasBuilderException e)
		{
		        Debug.e(e);
		}
	}
	private void loadTiledTextureRegion() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("menu/");
		ballTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.DEFAULT);
		mTiledTextureRegionMusic = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "musidonoff.png", 2, 1);
		mTiledTextureRegionSound= BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "soundonoff.png", 2, 1);
		mTiledTextureRegionX= BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "xonoff.png", 2, 1);
		mTiledTextureRegionUpdate= BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "updatescore1.png", 2, 1);
		mTiledTextureRegionTopScoreWait= BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "topscorewait.png", 2, 1);
		topscore_region= BitmapTextureAtlasTextureRegionFactory.createFromAsset(ballTextureAtlas, activity, "topscore.png");
		star1_region= BitmapTextureAtlasTextureRegionFactory.createFromAsset(ballTextureAtlas, activity, "star1.png");
		backbar_region= BitmapTextureAtlasTextureRegionFactory.createFromAsset(ballTextureAtlas, activity, "backbar.png");
		oldman_region= BitmapTextureAtlasTextureRegionFactory.createFromAsset(ballTextureAtlas, activity, "oldman.png");
		try 
		{
		    this.ballTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
		    this.ballTextureAtlas.load();
		} 
		catch (final TextureAtlasBuilderException e)
		{
		        Debug.e(e);
		}
/*		ballTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.DEFAULT);
		mTiledTextureRegionMusic = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "musidonoff.png", 2, 1);
		mTiledTextureRegionSound= BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "soundonoff.png", 2, 1);*/
	}

	public void loadHighscoreGraphics()
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("menu/");
		highscoreTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.DEFAULT);
		backscore_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(highscoreTextureAtlas, activity, "backscore.png");
		btnscore_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(highscoreTextureAtlas, activity, "btnscore.png");
		bar_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(highscoreTextureAtlas, activity, "bar.png");
		try 
		{
		    this.highscoreTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
		    this.highscoreTextureAtlas.load();
		} 
		catch (final TextureAtlasBuilderException e)
		{
		        Debug.e(e);
		}
	}
	
	public void loadLoginGraphics()
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("menu/");
		loginTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.DEFAULT);
		backlogin_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(loginTextureAtlas, activity, "backscore.png");
		btnlogin_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(loginTextureAtlas, activity, "btnlogin.png");
		btnregister_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(loginTextureAtlas, activity, "btnregister.png");
		try 
		{
		    this.loginTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
		    this.loginTextureAtlas.load();
		} 
		catch (final TextureAtlasBuilderException e)
		{
		        Debug.e(e);
		}
	}
	@SuppressWarnings("static-access")
	public void createSharePreference()
	{
		audioOptions = activity.getSharedPreferences("audio", activity.MODE_PRIVATE);
		audioEditor = audioOptions.edit();
		if (!audioOptions.contains("musicOn")){
			audioEditor.putBoolean("musicOn", true);
			audioEditor.putBoolean("effectsOn", true);
			audioEditor.commit();
		}
		scores = activity.getSharedPreferences("scores", activity.MODE_PRIVATE);
		scoresEditor = scores.edit();
		if (!scores.contains("WAV-4")){
			scoresEditor.putInt("WAV-4", 0);
			scoresEditor.putInt("WAV-3", 0);
			scoresEditor.putInt("WAV-2", 0);
			scoresEditor.putInt("WAV-1", 0);
			scoresEditor.putInt("WAV-0", 0);
			scoresEditor.putString("NWAV-4", "Player4");
			scoresEditor.putString("NWAV-3", "Player3");
			scoresEditor.putString("NWAV-2", "Player2");
			scoresEditor.putString("NWAV-1", "Player1");
			scoresEditor.putString("NWAV-0", "Player0");
			
			scoresEditor.commit();
		}
		login = activity.getSharedPreferences("login", activity.MODE_PRIVATE);
		loginEditor = login.edit();
		if (!login.contains("onlogin")){
			loginEditor.putBoolean("onlogin", false);
			loginEditor.putString("user", "unknown");
			loginEditor.putString("pass", "nopass");
			loginEditor.commit();
		}
	}
	@SuppressWarnings("static-access")
	public ArrayList<Score> getScoreFromSharedPreferences() {
		ArrayList<Score> arr = new ArrayList<Score>();
		scores = activity.getSharedPreferences("scores", activity.MODE_PRIVATE);
		String def = "Unknown";		 
		scoresEditor = scores.edit(); 
		int a;
		String sa;
		for(int i=0;i<5;i++){
			a = scores.getInt("WAV-"+i, 2);
			sa = scores.getString("NWAV-"+i, def);
			Score s = new Score(sa, a);
				arr.add(s);	
			 
		}	
		for(int i=0;i<arr.size();i++){
			Score s = arr.get(i);
			Log.d("result", s.player+":"+s.score+",rsm");
		}
		return arr;
	} 
	@SuppressWarnings("static-access")
	public void saveScores(Score[] s)
	{
		scores = activity.getSharedPreferences("scores", activity.MODE_PRIVATE);
		scoresEditor = scores.edit(); 
		for(int i=0;i<5;i++){	
			scoresEditor.putInt("WAV-"+i, s[i].score);
			scoresEditor.putString("NWAV-"+i, s[i].player);
		}
		 scoresEditor.commit();

	}
	public Score[] SortHighScoresByScore(ArrayList<Score> arr, int score)
	{
		Score scoreNew = new Score();
		Score[] scores = new Score[5];
		for(int i = 0; i <5; i++)
		{
			if(score>=arr.get(i).score){
				for(int j=4;j>i;j--){
					Score s = arr.get(j-1);
					scores[j] = s;
					//arr.set(j, arr.get(j+1));
				}
				scoreNew.score = score;
				//arr.set(i, scoreNew);
				scores[i] = scoreNew;	
				break;
			}
			else{
				Score s = arr.get(i);
				scores[i] = s;			
			}
			
		}
		return scores;
	}
	public Music musicGame;
	public void loadMusicGame()
	{
		try 
	    {
			MusicFactory.setAssetBasePath("mfx/");
			musicGame = MusicFactory.createMusicFromAsset(activity.getMusicManager(), activity, "themesong.ogg");
			musicGame.setLooping(true);
			musicGame.setVolume(1);
	    } 
	    catch (IOException e) 
	    {
	        e.printStackTrace();
	    }
	}
	public Sound sliceSound;
	private void loadMenuAudio() {
		try 
	    {
			SoundFactory.setAssetBasePath("mfx/");
			sliceSound = SoundFactory.createSoundFromAsset(activity.getSoundManager(), activity, "chemfinal.ogg");
	    } 
	    catch (IOException e) 
	    {
	        e.printStackTrace();
	    }
	}

	public void loadMenuFonts() { 
		FontFactory.setAssetBasePath("font/");
	    final ITexture mainFontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
	    font = FontFactory.createStrokeFromAsset(activity.getFontManager(), mainFontTexture, activity.getAssets(), "font.ttf", 50, true, Color.YELLOW, 2, Color.BLUE,false);
	    font.load();
	    fonttextx2 = FontFactory.createStrokeFromAsset(activity.getFontManager(), mainFontTexture, activity.getAssets(), "font.ttf", 40, true, Color.GREEN, 2, Color.BLUE,false);
	    fonttextx2.load();
	}

	private void loadGameGraphics() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		ballTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 2048, 2048, TextureOptions.DEFAULT);
		//a_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "a.png", 8, 3);
		boom_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(ballTextureAtlas, activity, "boom.png");
		playgame_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(ballTextureAtlas, activity, "playpause.png");
		restartgame_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(ballTextureAtlas, activity, "restart.png");
		score_region= BitmapTextureAtlasTextureRegionFactory.createFromAsset(ballTextureAtlas, activity, "score.png");
		quitgame_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(ballTextureAtlas, activity, "boomquit.png");
		titlepausegame_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(ballTextureAtlas, activity, "pause.png");
		
		star_region= BitmapTextureAtlasTextureRegionFactory.createFromAsset(ballTextureAtlas, activity, "star.png");
		//b_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "b.png", 8, 3);
		mTextureRegionLevel = BitmapTextureAtlasTextureRegionFactory.createFromAsset(ballTextureAtlas, activity.getAssets(), "level1.png");
		mTiledTextureRegionPlay= BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "playpause.png", 2, 1);
		mTiledTextureRegionResume= BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "resume.png", 2, 1);
		
		mTiledTextureRegionRingLeader = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "enemy.png", 3, 4);
		mTiledTextureRegionRingLeadergiftbox1 = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "giftbox.png", 6, 3);
		mTiledTextureRegionA = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "a1.png", 2, 1);
		mTiledTextureRegionB = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "b1.png", 2, 1);
		mTiledTextureRegionC = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "c1.png", 2, 1);
		mTiledTextureRegionD = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "d1.png", 2, 1);
		mTiledTextureRegionE = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "e1.png", 2, 1);
		mTiledTextureRegionF = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "f1.png", 2, 1);
		mTiledTextureRegionG = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "g1.png", 2, 1);
		mTiledTextureRegionH = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "h1.png", 2, 1);
		mTiledTextureRegionI = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "i1.png", 2, 1);
		mTiledTextureRegionJ = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "j1.png", 2, 1);
		mTiledTextureRegionK = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "k1.png", 2, 1);
		mTiledTextureRegionM = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "m1.png", 2, 1);
		mTiledTextureRegionN = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(ballTextureAtlas, activity.getAssets(), "n1.png", 2, 1);
		try 
		{
		    this.ballTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
		    this.ballTextureAtlas.load();
		} 
		catch (final TextureAtlasBuilderException e)
		{
		        Debug.e(e);
		}
		
		backgameTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.DEFAULT);
		game_background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgameTextureAtlas, activity, "backgame.png");
		try 
		{
			this.backgameTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
			this.backgameTextureAtlas.load();
		} 
		catch (final TextureAtlasBuilderException e)
		{
		        Debug.e(e);
		}
		
		textSpecialTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.DEFAULT);
		bananacold_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textSpecialTextureAtlas, activity, "bananacold.png");
		bananax2_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textSpecialTextureAtlas, activity, "bananax2.png");
		bananafrenzy_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textSpecialTextureAtlas, activity, "bananafrenzy.png");
		boomspec_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textSpecialTextureAtlas, activity, "bomspec.png");
		cong10_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textSpecialTextureAtlas, activity, "cong10.png");
		x2_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textSpecialTextureAtlas, activity, "x2.png");
		tru10_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textSpecialTextureAtlas, activity, "tru10.png");
		streak_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textSpecialTextureAtlas, activity, "streak1.png");
		offline_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textSpecialTextureAtlas, activity, "offline.png");
		try 
		{
			this.textSpecialTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
			this.textSpecialTextureAtlas.load();
		} 
		catch (final TextureAtlasBuilderException e)
		{
		        Debug.e(e);
		}
		loadText();
	}
	public void loadText()
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		textTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 2048, 2048, TextureOptions.DEFAULT);
		a_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(textTextureAtlas, activity.getAssets(), "a.png", 6, 4);
		b_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(textTextureAtlas, activity.getAssets(), "b.png", 6, 4);
		c_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(textTextureAtlas, activity.getAssets(), "c.png", 6, 4);
		h_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(textTextureAtlas, activity.getAssets(), "h.png", 6, 4);
		r_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(textTextureAtlas, activity.getAssets(), "r.png", 6, 4);
		t_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(textTextureAtlas, activity.getAssets(), "t.png", 6, 4);
		rLeft = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(textTextureAtlas, activity.getAssets(), "rleft.png", 6, 4);
		rRight = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(textTextureAtlas, activity.getAssets(), "rright.png", 6, 4);
		freeze_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textTextureAtlas, activity, "freeze.png");
		frenzy_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textTextureAtlas, activity, "frenzy.png");
		//frenzzy_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textTextureAtlas, activity, "frenzzy.png");
		
		try 
		{
		    this.textTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
		    this.textTextureAtlas.load();
		} 
		catch (final TextureAtlasBuilderException e)
		{
		        Debug.e(e);
		}
	}
	private void loadGameFonts() {
		
	}
	public Sound sliceWind,nemlemSound;
	private void loadGameAudio() {
		try 
	    {
			SoundFactory.setAssetBasePath("mfx/");
			sliceWind = SoundFactory.createSoundFromAsset(activity.getSoundManager(), activity, "chemgio.ogg");
			nemlemSound = SoundFactory.createSoundFromAsset(activity.getSoundManager(), activity, "nemlen.ogg");
	    } 
	    catch (IOException e) 
	    {
	        e.printStackTrace();
	    }
	}

	public Score[] SortHighScoresByScore(ArrayList<Score> arr, Score score) {
		Score scoreNew = new Score();
		Score[] scores = new Score[5];
		for (int i = 4; i > -1; i--) {
			if (score.score > arr.get(i).score) {
				scores[i] = score;
				for (int j = i - 1; j > -1; j--) {
					scores[j] = arr.get(j + 1);
				}
				// if (i==4) newTop = true;
				break;
			} else {
				scores[i] = arr.get(i);
			}

		}
		return scores;
	}
	public void loadSplashScreen() {
		/*
		 * BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		 * splashTextureAtlas = new
		 * BitmapTextureAtlas(activity.getTextureManager(), 256, 256,
		 * TextureOptions.BILINEAR); splash_region =
		 * BitmapTextureAtlasTextureRegionFactory
		 * .createFromAsset(splashTextureAtlas, activity, "splash", 0, 0);
		 * splashTextureAtlas.load();
		 */
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		/*
		 * Create a texture atlas suitable for the hill.png. The hill image used
		 * for this recipe is equal to 800x75 pixels in dimension*/
		 

		splashTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), activity.CAMERA_WIDTH, activity.CAMERA_HEIGHT,TextureOptions.DEFAULT);
		splash_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(splashTextureAtlas, activity,
						"background.png");
		try {
			this.splashTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 0, 0));
			this.splashTextureAtlas.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}
		
	}

	public void unloadSplashScreen() {
		splashTextureAtlas.unload();
		splash_region = null;
	}

	public static void prepareManager(Engine engine, GameActivity activity,
			BoundCamera camera, VertexBufferObjectManager vbom) {
		getInstance().engine = engine;
		getInstance().activity = activity;
		getInstance().camera = camera;
		getInstance().vbom = vbom;
	}
	public void playMusic()
	{
		if(audioOptions.getBoolean("musicOn", true)){
			if(!musicGame.isPlaying()){
				musicGame.play();
			}else{
				musicGame.resume();
			}
		}
	}
	public void playSoundSlice()
	{
		if(audioOptions.getBoolean("effectsOn", true)){
			sliceSound.play();
		}		
	}
	public void playSoundWind()
	{
		if(audioOptions.getBoolean("effectsOn", true)){
			sliceWind.play();
		}		
	}
	public void unloadMenuTextures()
	{
	    menuTextureAtlas.unload();
	}
	    
	public void loadMenuTextures()
	{
		ballTextureAtlas.load();
	    menuTextureAtlas.load();
	}
	public void unloadGameTextures()
	{
	    ballTextureAtlas.unload();	    
	}
	public void loadGameTextures()
	{
		ballTextureAtlas.load();
	}
	public void loadResultScene() {
		// TODO Auto-generated method stub
		
	}
	public void unloadTimeModeTextures() {
		// TODO Auto-generated method stub
		
	}
}
